﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_User_ViewResults : Form
    {
        SqlConnection coon = new SqlConnection(@"Data Source =DESKTOP-28VSNS5 ;Initial Catalog = Training_Project;                                               Uid = Lv;Pwd = 123456");
        #region  构造传递参数
        /// <summary>
        /// 构造传参数
        /// </summary>
        /// <param name="str"></param>
        public Pro_User_ViewResults(string str)
        {
            InitializeComponent();
            txt_Sid.Text = str;
            this.txt_Sid.Enabled = false;
        }
        #endregion
        #region  从数据库获取名字
        /// <summary>
        /// 获取名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pro_User_ViewResults_Load(object sender, EventArgs e)
        {
            var Sid = this.txt_Sid.Text;
            try
            {
                coon.Open();
                var sql = string.Format(@"select S.Stu_StuInfo_Name 
                                        from Stu_StuInfo S
                                        inner join Stu_Login T on(T.Sid =S.Sid)
                                        where T.UserName = '{0}'", Sid);
                var comm = new SqlCommand(sql, coon);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    var UserName = dr["Stu_StuInfo_Name"];
                    this.txt_Name.Text = UserName.ToString();
                    this.txt_Name.Enabled = false;
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region  点击查询成绩
        /// <summary>
        /// 点击查询成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Search_Click(object sender, EventArgs e)
        {
            var UserName = this.txt_Name.Text;
            var sql = string.Format(@"select S.Course_Name,SC.Score
                                       from Course S
                                       inner join Grade G on(G.Stu_StuInfo_Grade = S.Stu_StuInfo_Grade)
                                       inner join Score Sc on(Sc.Course_Num = S.Course_Num)
                                       inner join Stu_StuInfo ST on(St.Sid = SC.Sid)
                                       inner join Stu_Login L on(L.Sid = ST.Sid)
                                       where ST.Stu_StuInfo_Name  = '{0}'", UserName);
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(sql, coon);
            adp.Fill(ds);
            DataTable dt = ds.Tables[0];
            this.dataGridView1.DataSource = dt;
            //是否允许自动创建列
            this.dataGridView1.AutoGenerateColumns = false;
            //不允许出现空行
            this.dataGridView1.AllowUserToAddRows = false;
            //整行选中
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.dataGridView1.MultiSelect = false;
        }
        #endregion
    }
}
